const {getDmg,reduceResistanceK,lvSuppressK,objValueAdd } = require("../libs/apis.js")

const WEAPONS = require("./weapons.js");

const CLIPDATA = {
    fixedAtt: 19,
    reaction : 23,
    attRate: 5.8, 
    hpRate: 5.8, 
    fixedhp: 299,
    criticalRate: 3.9,
    criticalStrike: 7.8,
    defence: 23,
    defRate: 7.3,
    charging: 7.3
}

const CONFIG = {
    chance: 25,
    your_lv: 90,
    enemy_lv: 90,
    clips_main: {
        reaction: 187 * 3,
    },
    clips: ["reaction", "attRate", "criticalRate", "criticalStrike"],
    clips_initial: [
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["reaction", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalStrike"],
        ["charging", "attRate", "criticalRate", "criticalRate"]
    ],
    reactionK : 1.5,
    reactionDmgAdd: 0,
    buffs: [  
        { dmgRate: 15, attRate: 0, fixedAtt: 0 }   
    ]
}

let charaters = {
    units : {
        name: "万叶",  
        reaction: 115.2,
        charging: 0,
        defRate: 0,
        defence: 807,
        hp:13348,
        base: 297,    
        criticalRate: 5,    
        criticalStrike: 50,
        skills: {
            a: [
                function(data){},
            ]
        }
    },
}

calc(calc_clips_initial());

function calc(normal) {
    let charactersDmg = [];
    let count = 0;
    Object.values(WEAPONS).forEach(weapon => {
        charactersDmg.push(getDMG([charaters.units,weapon,{hp:4832,fixedAtt:311,...CONFIG.clips_main},normal], (data,second) => {
            count++;
            data = objValueAdd([data, ...CONFIG.buffs]);
            data.hp = data.hp*(1 + (data.hpRate||0)/100);
            if(weapon.skill) { weapon.skill(data); }
            
            data.reaction += 200
            let xialuoAdd = data.reaction * 0.2

            let rea = { reaction:data.reaction, reactionQ:CONFIG.reactionK, reactionDmgAdd:CONFIG.reactionDmgAdd }

            let EDmg = getDmg({ data, rate: 3.456}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let XiaLuoDmg = getDmg({ data:{...data, dmgRate:data.dmgRate + 32 + xialuoAdd }, rate: 4.0402}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let XiaLuoDmgZhuanhua = getDmg({ data:{...data, dmgRate:data.dmgRate + 32 + xialuoAdd - 15 }, rate: 2}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let QDmg = getDmg({ data, rate: 4.7232}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let QDmgKit = getDmg({ data, rate: 2.16}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //
            let QDmgRanse = getDmg({ data:{...data, dmgRate:data.dmgRate - 15 }, rate: 0.648}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv); //

            if(data.phyRate) { // 物理特效伤害
                data.phyDmg = getDmg({data:{...data, dmgRate:0}, rate:data.phyRate}).qw * reduceResistanceK(10) * lvSuppressK(CONFIG.your_lv, CONFIG.enemy_lv);  // 
            }
            let dmg = (EDmg + XiaLuoDmg + XiaLuoDmgZhuanhua) * 3 + QDmg + (QDmgKit + QDmgRanse) * 8 + (data.phyDmg||0)
            let attack = data.base *(1 + data.attRate/100) + data.fixedAtt;
            
            let point = data.reaction * 0.04
            return {point,EDmg,XiaLuoDmg,XiaLuoDmgZhuanhua, QDmg,dmg,QDmgKit,QDmgRanse , data, second, attack}
        }))
    })
    charactersDmg.sort((a,b)=>{
        return (b.point)-(a.point)
    });
    console.log(charactersDmg[2]);
    console.log(charactersDmg.map(v=>{
        return  `得分: ${v.data.weaponname} : ${v.point.toFixed(0)}`
    }));
    console.log(count);
}

function getDMG(dataarr, getRet){
    let chance = CONFIG.chance;
    let _ret = [];
    for (let index = 0; index <= chance; index++) {
        let c1 = index; 
        for (let index2 = 0; index2 <= chance - index; index2++) {
            let c2 = index2;
            for (let index3 = 0; index3 <= chance-index-index2; index3++) {
                let c3 = index3;
                let c4 = chance - index - index2 - index3;
                let cs = [c1, c2, c3, c4];
                var tmpdata = {};
                CONFIG.clips.forEach((v, i) => {
                    tmpdata[v] = CLIPDATA[v] * cs[i]
                })
                _ret.push(tmpdata);
            }
        }
    }
    _ret = _ret.map(second => {
        let panel = objValueAdd([...dataarr, second]);
        return  getRet(panel, second)
    })
    _ret.sort((b,a)=> a.point-b.point)
    return _ret[0]
}

// 固定攻击力 19  防御力23 生命值299 5.3% 
function calc_clips_initial () {
    let clips = CONFIG.clips_initial.map(v => {
        let tmp = {};
        v.forEach(v2 => {
            tmp[v2] = CLIPDATA[v2]
        })
        return tmp
    })
    let data = {}
    clips.forEach(item => {
        for (const key in item) {
            data[key] = data[key]? (data[key]+item[key]) : item[key]
        }
    })
    return data
}
